# -*- coding: utf-8 -*-
# !/usr/bin/env python


import sys
import time
import logging
from threading import Thread
from apscheduler.schedulers.blocking import BlockingScheduler

sys.path.append('../')

from proxy.Util.utilFunction import validHttpUsefulProxy,validHttpsUsefulProxy,validUsefulProxy
from proxy.Manager.ProxyManager import ProxyManager
from proxy.Util.LogHandler import LogHandler


logging.basicConfig()


class ProxyRefreshSchedule(ProxyManager):
    """
    代理定时刷新
    """

    def __init__(self):
        ProxyManager.__init__(self)
        self.log = LogHandler('refresh_schedule')

    def validProxy(self):
        """
        验证http_raw_proxy代理, 将可用的代理放入http_useful_proxy_queue
        :return:
        """
        self.db.changeTable(self.raw_proxy_queue)
        raw_proxy = self.db.pop()
        self.log.info('%s start valid http Proxy_a' % time.ctime())
        # 计算剩余代理，用来减少重复计算
        remaining_proxies = self.db.getAll()
        while raw_proxy:
            if (raw_proxy not in remaining_proxies) and validUsefulProxy(raw_proxy):
                self.db.changeTable(self.useful_proxy_queue)
                self.db.put(raw_proxy)
                self.log.info('valid http Proxy_a: %s validation pass' % raw_proxy)
            else:
                self.log.debug('valid http Proxy_a: %s validation fail' % raw_proxy)
            self.db.changeTable(self.raw_proxy_queue)
            raw_proxy = self.db.pop()
            remaining_proxies = self.db.getAll()
        self.log.info('%s valid http Proxy_a complete' % time.ctime())

    # def validHttpsProxy(self):
    #     #验证https_raw_proxy代理, 将可用的代理放入https_useful_proxy_queue
    #     self.db.changeTable(self.https_raw_proxy)
    #     raw_proxy = self.db.pop()
    #     self.log.info('%s start valid https Proxy_a' % time.ctime())
    #     # 计算剩余代理，用来减少重复计算
    #     remaining_proxies = self.db.getAll()
    #     while raw_proxy:
    #         if (raw_proxy not in remaining_proxies) and validUsefulProxy(raw_proxy):
    #             self.db.changeTable(self.https_useful_proxy)
    #             self.db.put(raw_proxy)
    #             self.log.info('valid https Proxy_a: %s validation pass' % raw_proxy)
    #         else:
    #             self.log.debug('valid https Proxy_a: %s validation fail' % raw_proxy)
    #         self.db.changeTable(self.https_raw_proxy)
    #         raw_proxy = self.db.pop()
    #         remaining_proxies = self.db.getAll()
    #     self.log.info('%s valid https Proxy_a complete' % time.ctime())




def refreshPool():
    pp = ProxyRefreshSchedule()
    pp.validProxy()
    # thread1 = Thread(target=pp.validHttpProxy, args=())
    # thread2 = Thread(target=pp.validHttpsProxy, args=())
    # thread1.start()
    # thread2.start()
    # thread1.join()
    # thread2.join()



def main(process_num=30):
    p = ProxyRefreshSchedule()

    # 获取新代理
    p.refresh()

    # 检验新代理
    pl = []
    for num in range(process_num):
        proc = Thread(target=refreshPool, args=())
        pl.append(proc)

    for num in range(process_num):
        pl[num].start()

    for num in range(process_num):
        pl[num].join()


def run():
    # main()
    sched = BlockingScheduler()
    sched.add_job(main, 'interval', minutes=5)
    sched.start()
    # validProxy()


if __name__ == '__main__':
    run()
